package com.moyun.finance.service;

import com.moyun.finance.entity.Account;
import com.moyun.finance.entity.Customer;
import com.moyun.finance.entity.Product;
import com.moyun.finance.entity.Response;
import com.moyun.finance.repository.AccountRepository;
import com.moyun.finance.repository.CustomerRepository;
import com.moyun.finance.utils.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerService {
    @Autowired
    CustomerRepository cr;

    public Response save(Customer customer){
        customer.setId(BaseUtil.uuid());
        cr.save(customer);
        return new Response("添加成功!",true);
    }

    public Response update(Customer customer){
        cr.save(customer);
        return new Response("客户信息修改成功!",true);
    }

    public Response delete(Customer customer){
        cr.delete(customer);
        return new Response("客户删除成功",true);
    }

    //本公司所有客户
    public List<Customer> findAll(String companyId){
        List<Customer> customers =  cr.findByCompanyId(companyId);
        return customers;
    }

    public Customer findOneById(String customerId){
        return cr.findById(customerId).orElse(null);
    }

    public List<Customer> findByNameLike(String companyId, String customerName){
        return cr.findByCompanyIdAndCustomerNameLike(companyId, "%"+customerName+"%");
    }

    public List<Customer> findByAllType(String companyId, String allTypeId, String cusType){
        List<Customer> customers = new ArrayList<>();
        if(cusType == null || "".equals(cusType)){
            customers = cr.findByCompanyIdAndAllTypeId(companyId, allTypeId);
        }else{
            customers =  cr.findByCompanyIdAndAllTypeIdAndCustomerType(companyId, allTypeId, cusType);
        }
        return customers;
    }
}
